package com.heihx.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heihx.constant.MessageConstant;
import com.heihx.constant.StatusConstant;
import com.heihx.dto.CategoryDTO;
import com.heihx.dto.CategoryPageQueryDTO;
import com.heihx.entity.Category;
import com.heihx.entity.Dish;
import com.heihx.entity.Setmeal;
import com.heihx.exception.DeletionNotAllowedException;
import com.heihx.mapper.CategoryMapper;
import com.heihx.result.PageResult;
import com.heihx.service.CategoryService;
import com.heihx.service.DishService;
import com.heihx.service.SetMealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealService setMealService;

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        //分类默认状态为禁用
        category.setStatus(StatusConstant.DISABLE);

        save(category);
    }

    /**
     * 删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        //查看分类是否关联了菜品
        Long count = dishService.lambdaQuery()
                .eq(Dish::getCategoryId, id)
                .count();
        if (count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查看分类是否关联了套餐
        count = setMealService.lambdaQuery()
                .eq(Setmeal::getCategoryId, id)
                .count();
        if (count > 0){
            //当前分类下有菜品，不能删除
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        //删除分类
        removeById(id);
    }

    /**
     * 分页查询分类
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        Page<Category> page = Page.of(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        String name = categoryPageQueryDTO.getName();
        Integer type = categoryPageQueryDTO.getType();

        //构建查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<Category>()
                .like(StringUtils.isNotBlank(name),Category::getName,name)
                .eq(type != null,Category::getType,type)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getCreateTime);

        page(page,wrapper);
        return new PageResult(page.getTotal(),page.getRecords());
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        updateById(category);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        lambdaUpdate()
                .eq(Category::getId,id)
                .set(Category::getStatus,status)
                .update();
    }

    @Override
    public List<Category> list(Integer type) {
        return lambdaQuery()
                .eq(type != null, Category::getType, type)
                .eq(Category::getStatus, StatusConstant.ENABLE)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getCreateTime)
                .list();
    }
}
